package com.pan.behavioral.observer.traffic.lane.impl;

import com.pan.behavioral.observer.traffic.lane.Lane;
import com.pan.behavioral.observer.pattern.Subject;
import com.pan.behavioral.observer.traffic.TrafficLight;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NorthSouthLane extends Lane {



    private Pattern trafficSignalPattern = Pattern.compile("(?<=north-south:\\s)((?i)red|green|yellow)(?=;)");

    @Override
    public void receive() {
        String status = super.getTrafficLightStatus();
        Matcher matcher = trafficSignalPattern.matcher(status);
        if (matcher.find()) {
            String trafficSignal = matcher.group();
            super.updateLaneStatus(trafficSignal);
            System.out.println("North-South Lane: " + super.getLaneStatus());
        } else {
            throw new RuntimeException("Unrecognized Traffic Light");
        }
    }
}
